package com.weir.quarkus.commons.utils;
import java.util.Random;

public class LogisticsNumberGenerator {
    public static void main(String[] args) {
        String logisticsNumber = generateLogisticsNumber();
        System.out.println("生成的物流单号为：" + logisticsNumber);
    }
    
    public static String generateLogisticsNumber() {
        // 定义物流单号的前缀
        String prefix = "YT";
        
        // 生成随机的订单信息
        String orderInfo = generateRandomOrderInfo();
        
        // 生成校验码
        int checkCode = generateCheckCode(orderInfo);
        
        // 合并前缀、订单信息和校验码，生成完整的物流单号
        String logisticsNumber = prefix + orderInfo + checkCode;
        
        return logisticsNumber;
    }
    
    public static String generateRandomOrderInfo() {
        // 定义可选的订单信息字符集
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        
        // 生成随机的订单信息，长度为8
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        
        for (int i = 0; i < 8; i++) {
            int index = random.nextInt(chars.length());
            sb.append(chars.charAt(index));
        }
        
        return sb.toString();
    }
    
    public static int generateCheckCode(String orderInfo) {
        // 计算订单信息的校验码，这里简单地将订单信息的每个字符的ASCII码相加并取余10
        int sum = 0;
        
        for (int i = 0; i < orderInfo.length(); i++) {
            sum += orderInfo.charAt(i);
        }
        
        int checkCode = sum % 10;
        
        return checkCode;
    }
}